﻿using System.ComponentModel;
using System.Drawing;
using WindowsAPI.Hooks;

namespace WindowsAPI
{
    [DefaultEvent("ButtonEvent"), DefaultProperty("Enable")]
    [ToolboxBitmap(typeof(MouseComponent))]
    public partial class MouseComponent : Component
    {
        /* --------------------------------------------------------------------------------------------- */

        /// <summary>Событие, происходящее при любых сообщениях мыши</summary>
        public event MouseEventHandler Event;
        /// <summary>Источник события сообщения мыши</summary>
        /// <param name="Args">Аргументы события с параметрами сообщения</param>
        protected virtual void OnEvent(MouseEventArgs Args)
        {
            var Handlers = Event;
            if(Handlers != null)
                Handlers.Invoke(this, Args);
        }
        private void OnEvent(Mouse.Message Msg, Mouse.MsllHookStruct Struct)
        {
            var args = new MouseEventArgs(Msg, Struct);
            OnEvent(args);

            switch(Msg)
            {
                case Mouse.Message.LeftButtonDown:
                case Mouse.Message.LeftButtonUp:
                case Mouse.Message.RightButtonDown:
                case Mouse.Message.RightButtonUp:
                case Mouse.Message.MiddleButtonDown:
                case Mouse.Message.MiddleButtonUp:
                    OnButtonEvent(MouseButtonEventArgs.FromMouseEventArgs(args));
                    return;

                case Mouse.Message.MouseMove:
                    OnMoveEvent(MouseMoveEventArgs.FromMouseEventArgs(args));
                    return;

                case Mouse.Message.MouseWheel:
                    OnWeelEvent(MouseWeelEventArgs.FromMouseEventArgs(args));
                    return;
            }

        }

        /// <summary>Событие происходит при перемещении мыши</summary>
        public event MouseMoveEventHandler MoveEvent;
        protected virtual void OnMoveEvent(MouseMoveEventArgs Args)
        {
            var lv_Handlers = MoveEvent;
            if(lv_Handlers != null) lv_Handlers.Invoke(this, Args);
        }

        public event MouseButtonEventHandler ButtonEvent;
        protected virtual void OnButtonEvent(MouseButtonEventArgs Args)
        {
            var lv_Handlers = ButtonEvent;
            if(lv_Handlers != null)
                lv_Handlers.Invoke(this, Args);
        }

        public event MouseWeelEventHandler WeelEvent;
        protected virtual void OnWeelEvent(MouseWeelEventArgs Args)
        {
            var lv_Handlers = WeelEvent;
            if(lv_Handlers != null) lv_Handlers.Invoke(this, Args);
        }

        /* --------------------------------------------------------------------------------------------- */

        private readonly MouseHookLL f_MouseHookLL = new MouseHookLL();

        /* --------------------------------------------------------------------------------------------- */

        [DefaultValue(false)]
        public bool Enable { get { return f_MouseHookLL.Enable; } set { f_MouseHookLL.Enable = value; } }

        /* --------------------------------------------------------------------------------------------- */

        public MouseComponent() : this(null) { }

        public MouseComponent(IContainer container)
        {
            if(container != null)
                container.Add(this);

            InitializeComponent();
            f_MouseHookLL.Event += OnMouseHookEvent;
        }

        /* --------------------------------------------------------------------------------------------- */

        private void OnMouseHookEvent(object sender, HookEventArgs e)
        {
            var E = (MouseHookEventArgs)e;
            OnEvent(E.Message, E.Info);
        }

        /* --------------------------------------------------------------------------------------------- */
    }
}
